package com.aiyige.page.my.localVideo;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.player.PlayerInfo;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.TimeUtils;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/10/17.
 */

public class MyLocalMovieAdapter extends BaseQuickAdapter<LocalMovieInfo, BaseViewHolder> {

    private boolean isEdit = false;

    public MyLocalMovieAdapter() {
        super(new LinkedList<LocalMovieInfo>());
    }

    public DeleteCallback getCallback() {
        return callback;
    }

    public void setCallback(DeleteCallback callback) {
        this.callback = callback;
    }

    DeleteCallback callback;

    @Override
    protected void convert(BaseViewHolder helper, LocalMovieInfo item) {
        ((MyHolder) helper).bindData(item);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = new MyHolder(getItemView(R.layout.item_local_movie, parent));
        return baseViewHolder;
    }


    public void onViewClicked(LocalMovieInfo data) {
        if (isEdit) {
            data.setSelected(!data.isSelected());
            notifyDataSetChanged();
            // 如果全选状态下其中一项手动unselect，那么全选图标设置为unselect
            if (!data.isSelected() && callback != null) {
                callback.unSelectCallback();
            }
        } else {
            List<PlayerInfo> playList = new ArrayList<>();
            playList.add(new PlayerInfo(data.getPath(),data.getFileName(), data.getImagePath(),true));
            ARouter.getInstance().build(ARouterConfig.PlayerPage).withSerializable("playList",(Serializable) playList).navigation();
        }
    }

    public class MyHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_selection)
        ImageView ivItemSelection;
        @BindView(R.id.rl_item_selection)
        RelativeLayout rlItemSelection;
        @BindView(R.id.rl_item_content)
        RelativeLayout rlItemContent;
        @BindView(R.id.image)
        ImageView imageView;
        @BindView(R.id.time)
        TextView timeView;
        @BindView(R.id.title)
        TextView titleView;
        @BindView(R.id.duration)
        TextView duration;





        public MyHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(final LocalMovieInfo data) {
            rlItemSelection.setVisibility(isEdit ? View.VISIBLE : View.GONE);
            ivItemSelection.setSelected(data.isSelected());
            titleView.setText(data.getFileName());
            timeView.setText(TimeUtils.getStandardDate((data.getDateTime())+""));
            duration.setText(TimeUtils.getDurationTime(data.getDuration()));
            GlideUtil.with(mContext).big().loadCover(data.getPath()).into(imageView);
            rlItemContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onViewClicked(data);
                }
            });
            rlItemSelection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onViewClicked(data);
                }
            });
            ivItemSelection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onViewClicked(data);
                }
            });

        }
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
        if (!isEdit) {
            unSelectAll();
        }
        notifyDataSetChanged();
    }


    public void selectAll() {
        List<LocalMovieInfo> list = getData();
        if (list == null || list.isEmpty()) {
            return;
        }
        for (LocalMovieInfo entity : list) {
            entity.setSelected(true);
        }
        notifyDataSetChanged();
    }

    public void unSelectAll() {
        List<LocalMovieInfo> list = getData();
        if (list == null || list.isEmpty()) {
            return;
        }
        for (LocalMovieInfo entity : list) {
            entity.setSelected(false);
        }
        notifyDataSetChanged();
    }

    public List<LocalMovieInfo> getSelectedList() {
        List<LocalMovieInfo> list = getData();
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<LocalMovieInfo> selectedList = new ArrayList<>();
        for (LocalMovieInfo entity : list) {
            if (entity.isSelected()) {
                selectedList.add(entity);
            }
        }
        return selectedList;
    }

    public void deleteSelected() {
        delete();
        ToastX.show(mContext.getString(R.string.delete_success));
    }

    private void showDialog() {
        ((BaseActivity) mContext).showLoading();
    }

    private void dismissDialog() {
        ((BaseActivity) mContext).hideLoading();
    }

    public void delete() {
        List<LocalMovieInfo> list = getData();
        if (list == null || list.isEmpty() || getSelectedList() == null
                || getSelectedList().isEmpty()) {
            return;
        }
        list.removeAll(getSelectedList());
        notifyDataSetChanged();
        if(callback != null)
        callback.deleteCallback();
    }


    public interface DeleteCallback {
        void deleteCallback();
        void unSelectCallback();
    }


}
